package com.neulogistics.contrib.warehouse.api.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blackto.lib.mediat.core.IMediator;
import com.neulogistics.contrib.warehouse.api.application.command.PurchaseCommand;
import com.neulogistics.contrib.warehouse.api.application.command.UpdateInventoryCommand;
import com.neulogistics.contrib.warehouse.api.application.query.IInventoryQueryService;
import com.neulogistics.contrib.warehouse.api.application.query.InventoryQuery;
import com.neulogistics.contrib.warehouse.api.application.viewmodel.InventoryViewModel;
import com.neulogistics.infrastructure.common.serviceresult.BaseResponse;
import com.neulogistics.infrastructure.common.serviceresult.ErrorCode;
import com.neulogistics.infrastructure.common.serviceresult.ResultUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Positive;

@RestController
@Api(tags = "库存管理")
@RequestMapping("/warehouse/inventory")
public class InventoryController {
    @Resource
    IMediator mediator;

    @Resource
    private IInventoryQueryService inventoryQueryService;

    @ApiOperation("主动进货")
    @PostMapping("/purchase")
    public BaseResponse<Boolean> purchase(@RequestBody @Validated @NotNull PurchaseCommand command){
        BaseResponse<Boolean> baseResponse = mediator.send(command);

        return baseResponse == null ? ResultUtils.error(ErrorCode.SYSTEM_ERROR) : baseResponse;
    }

    @ApiOperation("修改库存信息")
    @PostMapping("/update")
    public BaseResponse<Boolean> update(@RequestBody @NotNull UpdateInventoryCommand command){

        BaseResponse<Boolean> baseResponse = mediator.send(command);

        return baseResponse == null ? ResultUtils.error(ErrorCode.SYSTEM_ERROR) : baseResponse;
    }

    @ApiOperation("根据id查询")
    @GetMapping("/get/{id}")
    public BaseResponse<InventoryViewModel> get(@PathVariable(value = "id") @NotNull @Positive Integer id){
        // 已校验数据格式
        InventoryViewModel inventoryViewModel = inventoryQueryService.getById(id);

        return ResultUtils.success(inventoryViewModel);
    }

    @ApiOperation("分页查询库存")
    @PostMapping("/page")
    public BaseResponse<Page<InventoryViewModel>> page(@RequestBody @Validated @NotNull InventoryQuery inventoryQuery){

        Page<InventoryViewModel> viewModelPage = inventoryQueryService.page(inventoryQuery);

        return ResultUtils.success(viewModelPage);
    }
}
